package per.poker.gonghua;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Stream;

@Slf4j
public class PokerGen {

    private static final List<String> POKERS = new LinkedList<>();

    static void genNewCard() {

        POKERS.clear();
        String[] colors = {"♥", "♠", "♣", "♦"};
        String[] numbers = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
        for (String number : numbers) {
            for (String color : colors) {
                POKERS.add(color + number);
            }
        }
        POKERS.add("大王");
        POKERS.add("小王");
        do {
            log.info("----洗牌----");
            Collections.shuffle(POKERS);
        } while (ObjectUtil.equals("大王", CollUtil.getLast(POKERS)) ||
                ObjectUtil.equals("小王", CollUtil.getLast(POKERS)));
    }

    static Map<String, List<String>> distributeCard() {

        Map<String, List<String>> map = new HashMap<>(8);
        List<List<String>> ls = CollUtil.split(POKERS.subList(0, 24), 6);

        map.put("rest", POKERS.subList(24, POKERS.size()));
        //map.put("rest", CollUtil.newArrayList("♣10","♦7","♣5","♣9","♥4","♥3","大王","♥7","♣A","♦8","♣2","♦5","♥5","♠A","♠5","♣K","♦9","♦Q","♦6","♦J","♠2","♠K","♦4","♠9","♥J","♠3","♠7","♥6","♣8","♠8"));

        String mainColor = CollUtil.getLast(map.get("rest")).substring(0, 1);

//        ls.get(0).sort((o1, o2) -> compareCard(o1, o2, mainColor));
//        ls.get(1).sort((o1, o2) -> compareCard(o1, o2, mainColor));
//        ls.get(2).sort((o1, o2) -> compareCard(o1, o2, mainColor));
//        ls.get(3).sort((o1, o2) -> compareCard(o1, o2, mainColor));

        sortCard(ls.get(0), mainColor);
        sortCard(ls.get(1), mainColor);
        sortCard(ls.get(2), mainColor);
        sortCard(ls.get(3), mainColor);

        map.put("east", ls.get(0));
        map.put("south", ls.get(1));
        map.put("west", ls.get(2));
        map.put("north", ls.get(3));

        print(map);

//        map.put("east", CollUtil.newArrayList("♥9","♥Q","♣6","♣J","♦3","♠6"));
//        map.put("south", CollUtil.newArrayList("♥K","♦10","♦K","♠4","♠J","♠Q"));
//        map.put("west", CollUtil.newArrayList("♥8","♥10","♥A","♣3","♣Q","♦A"));
//        map.put("north", CollUtil.newArrayList("♥2","♣4","♣7","♦2","♠10"));

        return map;
    }

    public static void sortCard(List<String> cardList, String mainColor) {
        //相同花色按照大小排序，然后分花色

        List<String> heitao = new LinkedList<>();
        List<String> hongtao = new LinkedList<>();
        List<String> meihua = new LinkedList<>();
        List<String> fangpian = new LinkedList<>();
        List<String> joker = new LinkedList<>();
        for (String card : cardList) {
            String color = card.substring(0, 1);
            switch (color) {
                case "♠":
                    heitao.add(card);
                    break;
                case "♥":
                    hongtao.add(card);
                    break;
                case "♣":
                    meihua.add(card);
                    break;
                case "♦":
                    fangpian.add(card);
                    break;
                default:
                    joker.add(card);
            }
        }
        heitao.sort((o1, o2) -> compareCard(o1, o2, mainColor));
        hongtao.sort((o1, o2) -> compareCard(o1, o2, mainColor));
        meihua.sort((o1, o2) -> compareCard(o1, o2, mainColor));
        fangpian.sort((o1, o2) -> compareCard(o1, o2, mainColor));

        List<String> cards = new LinkedList<>();

        List<String> mainCards = new LinkedList<>();
        Stream.of(heitao, hongtao, meihua, fangpian).forEach(l -> {

            if (!l.isEmpty() && ObjectUtil.equal(mainColor, l.get(0).substring(0, 1))) {
                mainCards.addAll(l);
            } else {
                cards.addAll(l);
            }
        });
        cards.addAll(mainCards);
        cards.addAll(joker);
        cardList.clear();
        cardList.addAll(cards);
    }

    static int compareCard(String card1, String card2, String mainColor) {

        //大王=小王＞主2＞其他2＞其他主牌＞其他花色牌
        CardEnum cardEnum1 = EnumUtil.getBy(CardEnum.class, e -> ObjectUtil.equal(card1.substring(1), e.getCode()) || ObjectUtil.equal(card1, e.getCode()));
        CardEnum cardEnum2 = EnumUtil.getBy(CardEnum.class, e -> ObjectUtil.equal(card2.substring(1), e.getCode()) || ObjectUtil.equal(card2, e.getCode()));
        String color1 = card1.substring(0, 1);
        String color2 = card2.substring(0, 1);

        if (cardEnum1 == CardEnum._JOKER_R && cardEnum2 == CardEnum._JOKER_B) {
            return 0;
        }
        if (cardEnum1 == CardEnum._JOKER_B && cardEnum2 == CardEnum._JOKER_R) {
            return 0;
        }

        if (cardEnum1 == CardEnum._JOKER_R || cardEnum1 == CardEnum._JOKER_B) {
            return 1;
        } else if (cardEnum2 == CardEnum._JOKER_R || cardEnum2 == CardEnum._JOKER_B) {
            return -1;
        }
        //主花色大于其他花色牌
        else if (ObjectUtil.equal(mainColor, color1) && ObjectUtil.notEqual(mainColor, color2)) {
            return 1;
        }
        //其他花色牌小于主花色
        else if (ObjectUtil.equal(mainColor, color2) && ObjectUtil.notEqual(mainColor, color1)) {
            return -1;
        }
        //非主花色，比大小
        else if (ObjectUtil.notEqual(mainColor, color1) && ObjectUtil.notEqual(mainColor, color2)) {
            if (ObjectUtil.equal(color1, color2)) {
                return cardEnum1.getNum().compareTo(cardEnum2.getNum());
            } else {
                return 0;
            }
        }
        //非相同花色，比大小
        else if (ObjectUtil.equal(color1, color2)) {
            return cardEnum1.getNum().compareTo(cardEnum2.getNum());
        } else {
            log.warn("card1:{},card2:{}", card1, card2);
            throw new RuntimeException("error");
        }
    }

    /*public static void main(String[] args) {
        System.out.println(compareCard("♠Q", "♥10", "♦"));
    }*/

    private static void print(Map<String, List<String>> map) {
        StringBuilder s1 = new StringBuilder();
        map.get("east").forEach(e -> s1.append("\"").append(e).append("\"").append(","));
        s1.delete(s1.length() - 1, s1.length());

        StringBuilder s2 = new StringBuilder();
        map.get("south").forEach(e -> s2.append("\"").append(e).append("\"").append(","));
        s2.delete(s2.length() - 1, s2.length());

        StringBuilder s3 = new StringBuilder();
        map.get("west").forEach(e -> s3.append("\"").append(e).append("\"").append(","));
        s3.delete(s3.length() - 1, s3.length());

        StringBuilder s4 = new StringBuilder();
        map.get("north").forEach(e -> s4.append("\"").append(e).append("\"").append(","));
        s4.delete(s4.length() - 1, s4.length());

        StringBuilder s5 = new StringBuilder();
        map.get("rest").forEach(e -> s5.append("\"").append(e).append("\"").append(","));
        s5.delete(s5.length() - 1, s5.length());

        log.info("east:{}", s1);
        log.info("south:{}", s2);
        log.info("west:{}", s3);
        log.info("north:{}", s4);
        log.info("rest:{}", s5);
    }

}
